__author__ = 'Billy.Zhou'


class BaseRecord(object):
    def __init__(self, data):
        self.data = data

    def __getattr__(self, item):
        return self._get_optional_field(item)

    def _get_optional_field(self, *args):
        field = self.data
        for arg in args:
            if arg in field:
                field = field[arg]
            else:
                return None
        return field

    @property
    def id_str(self):
        return str(self.data['_id'])


class ExtraBase(object):
    def func(self):
        print 'extra base func'

    @staticmethod
    def static_func():
        print 'static func call'


class SubRecordTemp(BaseRecord, ExtraBase):
    def __getattr__(self, item):
        args = (item,)
        if 'special_item' == item:
            args = ('spec', 'item')
        return self._get_optional_field(*args)


d = {'name': 'zjx', 'age': 30, 'wx': {'oid': '123'}, '_id': 123456}
r = SubRecordTemp(d)
print r.name
print r.age
print r.wx_oid
print r.id_str


# @property
# def _uid(self):
#     return self._uid


class Base(object):
    def __init__(self, *args, **kwargs):
        if 'handler' in kwargs:
            self._uid = 'foo'


class Sub(Base):
    pass


# Sub(handler2=None)._uid

def __init__(self):
    super(Sub, self).__init__()


r.func()
r.static_func()

import pyconvert.pyconv

print dir(r)
# d.__dict__.update(name='zjx')
# print dir(d)
print r.__dict__


class Xml(object):
    def __init__(self, data):
        self.__dict__.update(data)


x = Xml({'name': 'zjx'})
x.age = 18
print pyconvert.pyconv.convert2XML(x).toxml(encoding='utf-8')
# print pyconvert.pyconv.convert2XML(o)

update = {'$set': {'name': {'f_n': 'jx', 'l_n': 'z'}, 'age': 16}, '$inc': {'age': 1}}


def update_modification(m):
    mm = dict()
    for k, v in m.iteritems():
        vv = dict()
        for f in v.iterkeys():
            vv['specifics.%s' % f] = v[f]
        mm[k] = vv
    return mm


print update_modification(update)
